<template>
  <j-modal
    switchFullscreen
    :title='title'
    :width='500'
    :visible='visible'
    :maskClosable='false'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>
    <template slot="footer">
      <a-button @click="handleCancel">取消</a-button>
      <a-button @click="handleOk" type="primary" v-if="!disableSubmit">确认</a-button>
    </template>

    <!--    <div class='info-line'>基础设置</div>-->
    <a-spin :spinning='confirmLoading'>
      <a-form :form='form'>
        <a-row :gutter='24'>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='活动编号'
              >
              <a-input placeholder='请输入' :disabled='disableSubmit' v-decorator="['activityCode']" />
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='经销商编号'
              >
              <a-input-search  v-decorator="['custCode',validatorRules.custCode]" :disabled='disableSubmit'
                              readOnly enter-button @search="custSearch('superiorUser')" />
<!--              <a-input  type='hidden' v-decorator="['customerCode']" />-->
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='经销商名称'
              >
              <a-input placeholder='请输入' disabled="true" v-decorator="['custName',validatorRules.custName]" />
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='活动销量'
              >
              <a-input placeholder='请输入' :disabled='disableSubmit' v-decorator="['activitySales',validatorRules.activitySales ]" />
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='活动销额'
              >
              <a-input placeholder='请输入' :disabled='disableSubmit' v-decorator="['activitySalesOf',validatorRules.activitySalesOf ]" />
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="备注">
              <a-textarea placeholder="请输入备注" :disabled='disableSubmit' v-decorator="['remarks']"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <proChose ref="prochoose" @okbro="okbropro"></proChose>
  </j-modal>
</template>

<script>
import proChose from '@comp/cn-components/ProChose'
import { getAction, httpAction, postAction } from '@/api/manage'
import { pick } from 'lodash'

export default {
  name: 'proMinPriceModal',
  data() {
    return {
      disableSubmit:false,
      sendPro: false,
      remarks: '',
      dataSurces: [],
      // dataSurces3: [],
      manobjList: [0],
      areaData: '',
      areaLinkage: '',
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        activityCode: {
          rules: [
            { required: true, message: ''},
            { pattern: /^[a-zA-Z][a-zA-Z0-9]*$/, message: ''},
          ]
        },
        customerCode: {
          rules: [
            { required: true, message: ''}
          ]
        },
        customerName: {
          rules: [
            { required: true, message: ''}
          ]
        },
        activitySales: {
          rules: [
            { required: true, message: ''}
          ]
        },
        activitySalesOf: {
          rules: [
            { required: true, message: ''}
          ]
        },
      },
      addOrUpdateUrl: window._CONFIG['domianPromotion'] + '/activitytarget/dmsActivityTarget/saveOrUpdate'
    }
  },
  components: {proChose },
  created() {
    // console.log(this.areaData);
  },
  methods: {
    okbropro(val, status, status2) {
      // console.log('数据',val)
      let objs = JSON.parse(JSON.stringify(val[0]))
      console.log('弹出框数据',objs)
      this.form.setFieldsValue({
        custCode: objs.custCode,
        custName: objs.custName
      })
    },
    add() {
      this.edit()
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, Object.keys(this.model)))//表单初始化，值源于this.model
      })
    },
    handleCancel() {
      this.$emit('close')
      this.visible = false
    },
    custSearch(status) {
      // console.log('数据',status)
      this.$refs.prochoose.rowKey='id'
      let formconfig = [
        {
          key: 'custCode',
          label: '经销商编号'
        },
        {
          key: 'custName',
          label: '经销商名称'
        }
      ]
      let tableconfig = {
        columns: [
          {
            title: '经销商编号',
            align: 'center',
            dataIndex: 'custCode',
            width: 120,
            ellipsis: true
          },
          {
            title: '经销商名称',
            align: 'center',
            dataIndex: 'custName',
            width: 120,
            ellipsis: true
          }
        ],
        list: window._CONFIG['domianURL'] + '/customerController/findPageList',
        multiple: false,
        columW: 8
      }
      let dataSource = []
      if(this.form.getFieldValue('superiorUser')){
        dataSource = [{
          userName: this.form.getFieldValue('superiorUser'),
          realName: this.form.getFieldValue('realName')
        }]
      }
      this.name = '经销商'
      this.$refs.prochoose.edit(formconfig, tableconfig,'superiorUser', 'realsname',dataSource)
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, value) => {
        if (!err) {
          let obj = Object.assign({}, this.model, value)
          console.log('提交数据',obj)
          postAction(this.addOrUpdateUrl, obj).then(res => {
            if (res.success) {
              that.$message.success(res.message, 2)
              that.$emit('ok')
              that.handleCancel()
            } else {
              that.$message.error(res.message, 2)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    }

  }
}
</script>

<style lang='less' scoped>
.info-line {
  display: flex;
  flex-direction: row;
}

.info-line:before {
  content: "";
  flex: 0.1;
  border-bottom: 1px solid #ebedee;
  margin: auto 4px;
}
.info-line:after {
  content: "";
  flex: 0.9;
  border-bottom: 2px solid #ebedee;
  margin: auto 4px;
}
</style>